package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/role")
@RestController
public class RoleController {


@Reference
private RoleService roleService;


    //分页查询
    @RequestMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){
        //获取查询条件
        String queryString = queryPageBean.getQueryString();
        //获取当前页
        Integer currentPage = queryPageBean.getCurrentPage();
        //获取每页展示数量
        Integer pageSize = queryPageBean.getPageSize();
        //调用pageService方法根据参数进行查询
        try {
            PageResult pageResult = roleService.pageQuery(currentPage, pageSize, queryString);
            return new Result(true,"查询角色信息成功",pageResult);
        }catch (Exception e){
            return  new Result(false,"查询角色信息失败");
        }
    }


    //添加角色
    @PostMapping("/add")
    public Result add(@RequestBody Role role, @RequestParam("permissionIds") Integer[] permissionIds,@RequestParam("menuIds") Integer[] menuIds){
        //保存角色基本信息
         roleService.add(role,permissionIds,menuIds);
        return  new Result(true,"添加成功");

    }



    //删除角色信息
    @RequestMapping("/delete")
    public  Result  deleteById(Integer id){
        try {
            roleService.deleteById(id);
            return new Result(true, "删除角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除角色失败");
        }
    }

    //根据ID查询角色基本信息
    @RequestMapping("/findById")
    public  Result findById(Integer id){
        try {
             Role role=roleService.findById(id);
             return  new Result(true,"c查询角色基本信息成功",role);
        }catch (Exception e){
            return  new Result(false,"查询角色基本信息失败");
        }
    }

    //查询角色和权限关联组信息
    @RequestMapping("/findRoleAsPermission")
    public Result findRoleAsPermission(Integer id){
        try {
           Integer[] roleAsPermissions=roleService.findRoleAsPermission(id);
           return new Result(true,"查询角色和权限关联组成功",roleAsPermissions);
        }catch (Exception e){

            return  new Result(false,"查询角色和权限关联组失败");
        }
    }

    //查询角色和菜单的关联组信息
    @RequestMapping("/findRoleAsMenu")
    public Result findRoleAsMenu(Integer id){
        try {
            Integer[] roleAsMenus=roleService.findRoleAsMenu(id);
            return new Result(true,"查询角色和菜单关联组成功",roleAsMenus);
        }catch (Exception e){

            return  new Result(false,"查询角色和菜单关联组失败");
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody Role role, @RequestParam("permissionIds") Integer[] permissionIds,@RequestParam("menuIds") Integer[] menuIds) {
        //保存角色基本信息
        try {
            roleService.update(role, permissionIds, menuIds);
            return new Result(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改角色信息失败");
        }
    }
    @GetMapping("/findAll")
    public Result findAll(){
        try{
            List<Role> roles = roleService.findAll();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,roles);
        }catch(Exception e){
            return new Result(false,MessageConstant.QUERY_ROLE_FAILED);
        }
    }
}
